package com.sitech.portal.server.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.dispatcher.mapper.ActionMapping;

import com.sitech.ibnms.core.JGridBaseAction;
import com.sitech.portal.server.service.IWorkPlanService;
import com.sitech.portal.system.domain.TblSYS_LOGINMSG;

/**
 * ClassName: com.sitech.ismp.portal.action.WorkPlanAction
 * @author liujhc
 * @since Mar 10, 2011
 */
public class WorkPlanAction extends JGridBaseAction {

	private IWorkPlanService workPlanService;
	private List resultList;
	private String flag;
    /**
     * 获取一天的所有任务计划
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String findAllWorkPlan() {
        String DATE = this.getRequest().getParameter("DATE");
        if (DATE == null) {
            DATE = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        }
        // 取得用户LOGIN_ID
        TblSYS_LOGINMSG user = this.getCurrentUser();
        // 获取用户今天的任务
        resultList = workPlanService.findAllWorkPlan(user.getLOGIN_ID(), DATE);
//        request.getSession().setAttribute("START_TIME_DATE", DATE);
        return SUCCESS;
    }

    /**
     * 子任务计划
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String findWorkPlanByParentId() {
        String PARENT_ID = this.getRequest().getParameter("PARENT_ID");
        resultList = workPlanService.findWorkPlanByParentId(PARENT_ID);
        return SUCCESS;
    }

    /**
     * 删除任务计划及其子计划
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String deleteWorkPlan() {
    	flag = "true";
    	try{
	        String TASK_ID = this.getRequest().getParameter("TASK_ID");
	        workPlanService.delWorkPlan(TASK_ID);
    	}catch(Exception e){
    		flag = e.getMessage();
    	}
        return SUCCESS;
    }

    /**
     * 创建或修改计划任务
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String saveOrUpdateWorkPlan() {
    	flag = "true";
    	try{
	        Map<String, String> wp = new HashMap(); //所有非空参数
	        Enumeration<String> en = this.getRequest().getParameterNames();
	        while (en.hasMoreElements()) {
	            String key = en.nextElement();
	            String param = this.getRequest().getParameter(key).trim();
	            if (!"".equals(param)) {
	                wp.put(key, param);
	            }
	        }
	        // 计算ALARM_MODE
	        String[] vals = this.getRequest().getParameterValues("ALARM_MODE");
	        if (vals != null) {
	            int ALARM_MODE = 0;
	            for (int i = 0; i < vals.length; i++) {
	                ALARM_MODE += Integer.parseInt(vals[i]);
	            }
	            wp.put("ALARM_MODE", "" + ALARM_MODE);
	        }
	
	        // 取得用户LOGIN_ID
	        TblSYS_LOGINMSG user = this.getCurrentUser();
	        wp.put("LOGIN_ID", user.getLOGIN_ID());
	        if (wp.get("TASK_ID") != null) {
	        	workPlanService.updateWorkPlan(wp);
	        } else {
	        	workPlanService.insertWorkPlan(wp);
	        }
    	}catch(Exception e){
    		flag = e.getMessage();
    	}
        return SUCCESS;
    }

    /**
     * 显示saveOrUpdateWorkPlan的表单
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String workPlanForm() {
        String TASK_ID = this.getRequest().getParameter("TASK_ID");
        if (TASK_ID != null && !"".equals(TASK_ID)) {
            workPlanService.findWorkPlanById(TASK_ID);
        }
        return SUCCESS;
    }

    /**
     * 任务计划的
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public String workPlanDetail() {
        String TASK_ID = this.getRequest().getParameter("TASK_ID");
        Map wp = new HashMap();
        if (TASK_ID != null && !"".equals(TASK_ID)) {
            wp = workPlanService.findWorkPlanById(TASK_ID);
        }
        return SUCCESS;
    }
}
